package com.qire.manhua.controller;

import android.view.View;

import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.FloorCard;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.Navigation;
import com.qire.common.support.base.BaseDialog;
import com.qire.common.support.base.EventController;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.manhua.BR;
import com.qire.manhua.activity.ComicDownloadSelectorActivity;
import com.qire.manhua.activity.helper.LoginRouter;
import com.qire.manhua.databinding.ActivityComicDownloadSelectorBinding;
import com.qire.manhua.model.viewModel.impl.ComicDownloadSelectorViewModel;
import com.qire.manhua.model.viewModel.impl.ComicDownloadSelectorViewModel.DownloadSelectorBatchItemViewModel;
import com.qire.manhua.model.viewModel.impl.ComicDownloadSelectorViewModel.DownloadSelectorChapterItemViewModel;
import com.qire.other.recorder.ComicActionRecorder;

import androidx.annotation.NonNull;
import androidx.databinding.ViewDataBinding;

public class ComicDownloadSelectorController extends EventController<ComicDownloadSelectorActivity> implements AutoVariable {

    private ComicDownloadSelectorViewModel viewModel;

    public ComicDownloadSelectorController(@NonNull ComicDownloadSelectorActivity lifecycleOwner) {
        super(lifecycleOwner);
    }

    @Override
    protected void bindView() {
        ActivityComicDownloadSelectorBinding viewDataBinder = owner.viewDataBinder();
        viewDataBinder.setComicDownloadSelectorController(this);
    }

    @Override
    public void bindVariable(ViewDataBinding viewBind) {
        viewBind.setVariable(BR.comicDownloadSelectorController, this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(ComicDownloadSelectorViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /*************************************************************************************************************************/
    /** 进入批量选择页首次点击操作时需要清除已选择选项 */
    private boolean firstSelect = false;

    public void backBtnOnClick(View view) {
        owner.onBackPressed();
    }

    public void chapterItemOnClick(View view) {
        viewModel.changeDownloadTipsVisible(View.GONE);
        DownloadSelectorChapterItemViewModel itemViewModel = ObjectUtil.as(view.getTag(), DownloadSelectorChapterItemViewModel.class);
        if(ObjectUtil.nonNull(itemViewModel)) {
            itemViewModel.toggleChoose();
        }
    }

    /**
     * 批量选择按钮单击事件
     * */
    public void batchChoiceItemOnClick(View view) {
        if(firstSelect) {
            viewModel.clearAllChoose();
            firstSelect = false;
        }

        DownloadSelectorBatchItemViewModel  groupViewModel = ObjectUtil.as(view.getTag(), DownloadSelectorBatchItemViewModel.class);
        if(ObjectUtil.nonNull(groupViewModel)) {
            groupViewModel.toggleChoose();
        }
    }
    /**
     * 打开批量选择面板
     * */
    public void openBatchChoicePanelBtnOnClick(View view) {
        viewModel.changeBatchChoosePanelVisible(View.VISIBLE);
        firstSelect = true;
    }
    /**
     * 关闭批量选择面板
     * */
    public void closeBatchChoicePanelBtnOnClick(View view) {
        viewModel.changeBatchChoosePanelVisible(View.GONE);
    }
    /**
     * 批量下载或批量购买按钮单击事件
     * */
    public void batchDownloadOnClick(View view) {
        if(LoginRouter.ROUTER.checkLoginRouteIfNeed()) {
            return;
        }
        viewModel.changeBatchChoosePanelVisible(View.GONE);
        viewModel.checkUnlockOrDownload();
    }

    public void closeUnlockDialogOnClick(View view) {
        BaseDialog dialog = ObjectUtil.as(view.getTag(), BaseDialog.class);
        if(ObjectUtil.nonNull(dialog)) {
            dialog.dismiss();
            view.setTag("");
        }
    }

    public void buyMembershipBtnOnClick(View view) {
        AntsRouter.ROUTER.buildCard(Navigation.Page.BuyMembershipPage).navigation();
        closeUnlockDialogOnClick(view);
    }
    public void buyUnlockBtnOnClick(View view) {
        viewModel.showDownloadUnlockDialog();
        closeUnlockDialogOnClick(view);
    }

    public void unlockBtnOnClick(View view) {
        if(viewModel.isInsufficientBalance()) {
            // 余额不足
            AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.BuyCoinsMarketDialogPage)
                    .withData("comicId", viewModel.comicBookId())
                    .navigation();


            // 行为数据统计
            ComicActionRecorder.build(ComicActionRecorder.count_recharge_show)
                    .setSource("下载金额不足充值按钮")
                    .doRecorde(view.getContext());
// todo 不知道有什么用的代码需要之后来判断是否可以清除
//            activity.subscribeEvent(CustomEvent.PayToCompleteEventTag,
//                    event -> {
//                        ComicDownloadPresenter presenter = PresenterFactory.build()
//                                .injection("viewModel",viewModel)
//                                .create(ComicDownloadPresenter.class);
//                        presenter.batchUnlockBusinessProcess();
//                        return false;
//                    });

        } else {
            // 可以解锁
            viewModel.batchBuy();
        }

        closeUnlockDialogOnClick(view);
    }

}
